package com.example.wc.logindemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;


public class WriteBlogActivity extends AppCompatActivity {

    private TextView mBack;
    private ImageButton mPictureImageButton;
    private EditText mName;
    private EditText mContent;
    private Button mWrite;
    private ImageButton mNameImageButton;
    private ImageButton mContentImageButton;
    private SpeechRecognizerDemo mSpeechRecognizerDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_write);

        mBack = (TextView) findViewById(R.id.back_title_write_blog_activity);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteBlogActivity.this, BlogsListActivity.class);
                startActivity(intent);
            }
        });

        mPictureImageButton = (ImageButton)findViewById(R.id.picture_image_button_write_blog_activity);
        mPictureImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteBlogActivity.this, PictureBlogActivity.class);
                startActivity(intent);
            }
        });

        mName = (EditText)findViewById(R.id.name_edit_text_write_blog_activity);
        mContent =(EditText)findViewById(R.id.content_edit_text_write_blog_activity);
        mWrite = (Button)findViewById(R.id.write_button_write_blog_activity);
        mWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
                volleyRequestDemo.volleyRequestDemo_POST_Add(WriteBlogActivity.this,
                        mName.getText().toString(), mContent.getText().toString(),
                        new VolleyRequestDemo.BlogPostListener() {
                            @Override
                            public void onBlogPosted() {
                                Intent i = new Intent(WriteBlogActivity.this, BlogsListActivity.class);
                                startActivity(i);
                            }
                        });
            }
        });

        mNameImageButton = (ImageButton) findViewById(R.id.speech_name_image_button);
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58babddc");
        if(mSpeechRecognizerDemo == null){
            mSpeechRecognizerDemo = new SpeechRecognizerDemo();
        }
        mNameImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(WriteBlogActivity.this, mName);
            }
        });

        mContentImageButton = (ImageButton) findViewById(R.id.speech_content_image_button);
        mContentImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(WriteBlogActivity.this, mContent);
            }
        });
    }

//    //TODO 开始说话：
//    private void btnVoice(final EditText editText) {
//        RecognizerDialog dialog = new RecognizerDialog(this,null);
//        dialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
//        dialog.setParameter(SpeechConstant.ACCENT, "mandarin");
//
//        dialog.setListener(new RecognizerDialogListener() {
//            @Override
//            public void onResult(RecognizerResult recognizerResult, boolean b) {
//                printResult(recognizerResult, editText);
//            }
//            @Override
//            public void onError(SpeechError speechError) {
//            }
//        });
//        dialog.show();
//        Toast.makeText(this, "请开始说话", Toast.LENGTH_SHORT).show();
//    }
//
//    //回调结果：
//    private void printResult(RecognizerResult results, EditText editText) {
//        String text = parseIatResult(results.getResultString());
//        // 自动填写地址
//        editText.append(text);
//    }
//
//    public static String parseIatResult(String json) {
//        StringBuffer ret = new StringBuffer();
//        try {
//            JSONTokener tokener = new JSONTokener(json);
//            JSONObject joResult = new JSONObject(tokener);
//
//            JSONArray words = joResult.getJSONArray("ws");
//            for (int i = 0; i < words.length(); i++) {
//                // 转写结果词，默认使用第一个结果
//                JSONArray items = words.getJSONObject(i).getJSONArray("cw");
//                JSONObject obj = items.getJSONObject(0);
//                ret.append(obj.getString("w"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return ret.toString();
//    }

}
